$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        data:null,
        id:null,
        init:function () {
            this.form = new xhForm($('#xh_form'));
            this.form.parse();
            this.id = xhTool.getQuery('iRTId');
            this.data = data;
            this.listener();
        },
        listener:function () {
            var $this = this;
            $('.sDevType').on('change',function () {
                $this.funObj().targetChange($(this));
            });

            //保存事件
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().submitForm();
            });

            //批量修改
            $('#bt_view_usage').click(function () {
                $this.funObj().allChange();
            });
            
            //部分替换
            $('#bt_view_replacePart').click(function () {
                $this.funObj().replacePart();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                targetChange:function(target) {
                    var devType = target.find("option:selected").text();
                    var devNumber = target.parent().parent().find('.sDevNumber').val();
                    $.each($this.data, function (i,item) {
                        if (item['sDevNumber'] == devNumber){
                            $this.data[i]['sDevType'] = devType;
                        }
                    });
                    $this.funObj().changeText();
                },
                submitForm:function () {
                    xhExtra.ajaxRequest('device/saveModifyRetTablet', {id:$this.id,mData:$this.data}, {}, function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                        window.top.xhTop.closeModal();
                    })
                },
                allChange:function () {
                    var dev = $('#allDev').val();
                    $('.sDevType').each(function () {
                        $(this).val(dev).trigger('change');
                    })
                },
                changeText:function () {
                    var numObj = {};
                    $.each($this.data, function (i,item) {
                        var devType = item['sDevType'];
                        if (numObj[devType]) {
                            numObj[devType] += 1;
                        } else {
                            numObj[devType] = 1;
                        }
                    });
                    var str= '';
                    $.each(numObj,function (name,num) {
                        str += name+'共'+num+'台;'
                    });
                    $('#title').text(str);
                },
                replacePart:function () {
                    var str = '';
                        str += '<select id="oldType" class="form-control" >\n';
                        str += '<option value="" selected style="display: none">请选择</option>';
                            $.each(prod,function (id,text) {
                                str += '<option value="'+id+'">'+text+'</option>';
                            });
                        str += '</select><br><p>替换</p>';
                        str += '<select id="newType" class="form-control" >\n';
                        str += '<option value="" selected style="display: none">请选择</option>';
                        $.each(prod,function (id,text) {
                            str += '<option value="'+id+'">'+text+'</option>';
                        });
                        str += '</select>';
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '替换',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '部分替换',
                        html: str,
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var oldType     = $('#oldType', window.top.document).val(),
                                    newType = $('#newType', window.top.document).val();
                                if (!oldType || !newType) {
                                    reject('需替换类型，或者类型为空');
                                } else {
                                    $this.funObj().replace(oldType,newType);
                                    resolve();
                                }
                            })
                        }
                    })
                },
                replace:function (oldType,newType) {
                    $('.sDevType').each(function () {
                        var nowType = $(this).val();
                        if (nowType == oldType) {
                            $(this).val(newType).trigger('change');
                        }
                    })
                }
            }
        }
    }
};